﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using eProject_SEM3_SecurityServices.ObjectClass;
using System.Data.SqlClient;
using System.Data;

namespace eProject_SEM3_SecurityServices.DataAccess
{
    public class DTaskOfDepartment
    {
        public bool NewTaskDepartmentAdd(OTaskOfDepartment taskOfdepartment)
        {
            SqlParameter[] parameters = new SqlParameter[]
            {                
               new SqlParameter("@departmentId",taskOfdepartment.DepartmentId),
               new SqlParameter("@serviceId",taskOfdepartment.ServiceId)
            };

            return DConnectSQL.ExecuteNonQuery("sp_AddnewTaskOfDepartment", CommandType.StoredProcedure, parameters);
        }

        public List<OTaskOfDepartment> getListFunctionOfDepartment()
        {
            List<OTaskOfDepartment> listFunctionOfDepartment = null;
            using (DataTable table = DConnectSQL.ExecuteSelectCommand("getListFunctionOfDepartment", CommandType.StoredProcedure))
            {
                if (table.Rows.Count > 0)
                {
                    listFunctionOfDepartment = new List<OTaskOfDepartment>();

                    foreach (DataRow row in table.Rows)
                    {
                        OTaskOfDepartment taskOfDepartment = new OTaskOfDepartment();
                        taskOfDepartment.ServiceId = int.Parse(row["serviceId"].ToString());
                        taskOfDepartment.DepartmentId = int.Parse(row["departmentId"].ToString());

                        listFunctionOfDepartment.Add(taskOfDepartment);
                    }
                }
            }
            return listFunctionOfDepartment;
        }
        public int getDepartmentId(int serviceId)
        {
            List<OTaskOfDepartment> listFunctionOfDepartment = null;
            int id = -1;
            using (DataTable table = DConnectSQL.ExecuteSelectCommand("select * from Function_Of_Department where serviceId = "+serviceId, CommandType.Text))
            {               
                if (table.Rows.Count > 0)
                {
                    listFunctionOfDepartment = new List<OTaskOfDepartment>();

                    foreach (DataRow row in table.Rows)
                    {
                        OTaskOfDepartment taskOfDepartment = new OTaskOfDepartment();
                        taskOfDepartment.ServiceId = int.Parse(row["serviceId"].ToString());
                        taskOfDepartment.DepartmentId = int.Parse(row["departmentId"].ToString());
                        id = taskOfDepartment.DepartmentId;
                        listFunctionOfDepartment.Add(taskOfDepartment);
                    }
                }
            }
            return id;
        }
    }
}